/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.PenggunaDAO;
import exatrez.dao.PersonalTaskDAO;
import exatrez.dao.PriorityCodeDAO;
import exatrez.dao.TaskFileDAO;
import exatrez.dao.TaskSummaryDAO;
import exatrez.manager.PriorityManager;
import exatrez.manager.TaskManager;
import exatrez.manager.UserManager;
import exatrez.model.Document;
import exatrez.model.Folder;
import exatrez.model.InfoAudit;
import exatrez.model.PersonalTask;
import exatrez.model.PriorityCode;
import exatrez.model.TaskFile;
import exatrez.model.TaskSummary;
import exatrez.model.User;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.Configuration;
import exatrez.stripes.config.AuthRequired;
import exatrez.stripes.util.EmailUtil;
import exatrez.stripes.util.FileUtil;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import java.io.File;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ErrorResolution;
import net.sourceforge.stripes.action.FileBean;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.security.action.Secure;
//import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author wan.fairul
 */
@UrlBinding("/personalTask")
@AuthRequired
public class PersonalAction extends BaseActionBean {

    private static Logger LOG = LoggerFactory.getLogger(PersonalAction.class);
    private List<User> userList;
    private List<PersonalTask> listOfTask;
    private List<PersonalTask> listTask;
    private List<PersonalTask> listTaskHistory;
    private List<PersonalTask> listTaskHistoryCoo;
    private List<TaskSummary> listTaskSummary;
    private List<TaskFile> listTaskfile;
    private List<PersonalTask> listOpenTask;
    private List<PersonalTask> listCloseTask;
    @Inject
    private UserManager userManager;
    private String q;
    private PersonalTask task;
    private TaskSummary taskSummary;
    private String id;
    private String priorityCode;
    @Inject
    private PriorityCodeDAO priorityCodeDAO;
    @Inject
    private PenggunaDAO penggunaDAO;
    @Inject
    private EmailUtil emailUtil;
    @Inject
    private TaskManager taskManager;
    @Inject
    private PersonalTaskDAO personalTaskDAO;
    private String msg;
    @Inject
    private TaskSummaryDAO taskSummaryDAO;
    @Inject
    private PriorityManager priorityManager;
    @Inject
    private TaskFileDAO taskFileDAO;
    private String priorityDayCount;
    private String dayLeft;
    private User staff;
    private static final String FOLDERNAME = "TASK_TEMP";
    @Inject
    Configuration conf;
    private FileBean document;
    private String forXlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    private String forDocx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    private String forPptx = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    private String forXls = "application/vnd.ms-excel";
    private String forDoc = "application/msword";
    private String forPpt = "application/vnd.ms-powerpoint";
    private String forPdf = "application/pdf";
    private String forVideoMov = "video/quicktime";
    private String forVideoMkv = "video/x-matroska";
    private String forVideoMp4 = "video/mp4";
    private String forVideoAvi = "video/x-msvideo";
    private String forVideoMpeg = "video/mpeg";
    private String forVideoWmv = "application/octet-stream";
    private String forVideoAsf = "video/x-ms-asf";
    private String forVideoFlv = "video/x-flv";
    private String forVideoRmvb = "application/vnd.rn-realmedia-vbr";
    private String forVideo3gp = "video/3gpp";

    @DefaultHandler
    public Resolution new_task() {
        task = new PersonalTask();
        return new JSP("personal/task.jsp");
    }

    public Resolution list_task() {
        task = new PersonalTask();
        listOfTask = taskManager.findAll();
        return new JSP("personal/list_task.jsp");
    }

    public Resolution history_task() {
        User user = getContext().getCurrentUser();
        if (user != null) {
            listTaskHistory = taskManager.findTaskUserClose(user);
        }
        return new JSP("personal/history_task.jsp");
    }

    public Resolution history_taskCoo() {
        listTaskHistoryCoo = taskManager.findTaskCooClose();
        return new JSP("personal/history_taskCoo.jsp");
    }

    public Resolution inbox() {
        User user = getContext().getCurrentUser();
        task = new PersonalTask();
        if (user != null) {
            listTask = taskManager.findTaskUser(user);
        }
        return new JSP("personal/inbox.jsp");
    }

    public Resolution searchListTask() {
        User user = getContext().getCurrentUser();
        listTask = taskManager.findTaskBySubject(task, user);
        return new JSP("personal/inbox.jsp");
    }

    public Resolution searchListTaskClose() {
        User user = getContext().getCurrentUser();
        listTaskHistory = taskManager.findTaskBySubjectClose(task, user);
        return new JSP("personal/history_task.jsp");
    }

    public Resolution searchListTaskCoo() {
        listOfTask = taskManager.findTaskBySubjectCoo(task);
        return new JSP("personal/list_task.jsp");
    }

    public Resolution searchListTaskCloseCoo() {
        listTaskHistoryCoo = taskManager.findListTaskCloseCoo(task);
        return new JSP("personal/history_taskCoo.jsp");
    }

    public Resolution viewListOpenTask() {
        listOpenTask = taskManager.findTaskBySubjectCoo(task);
        return new JSP("personal/view_opentask.jsp");
    }

    public Resolution viewListCloseTask() {
        listCloseTask = taskManager.findListTaskCloseCoo(task);
        return new JSP("personal/view_closetask.jsp");
    }

    public Resolution reply_form() {
        User user = getContext().getCurrentUser();
        id = getContext().getRequest().getParameter("id");
        staff = penggunaDAO.findById(user.getUserId());
        if (id != null) {
            task = personalTaskDAO.findById(Integer.parseInt(id));
            if (task.getDuedate() != null) {
                Date d1 = new Date();
                Date d2 = task.getDuedate();
                LOG.debug("dayLeft1" + d1.getTime());
                LOG.debug("dayLeft2" + d2.getTime());
                dayLeft = String.valueOf((d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000));
                LOG.debug(dayLeft);
            }
            listTaskSummary = taskManager.findSummaryTask(String.valueOf(task.getPersonalTaskId()));
        }
        return new JSP("personal/reply_form.jsp");
    }

    public Resolution reply_form_coo() {
        User user = getContext().getCurrentUser();
        id = getContext().getRequest().getParameter("id");
        if (id != null) {
            task = personalTaskDAO.findById(Integer.parseInt(id));
            staff = penggunaDAO.findById(task.getToUser());
            if (task.getDuedate() != null) {
                Date d1 = new Date();
                Date d2 = task.getDuedate();
                LOG.debug("dayLeft1" + d1.getTime());
                LOG.debug("dayLeft" + d2.getTime());
                dayLeft = String.valueOf((d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000));
                LOG.debug(dayLeft);
            }
            listTaskSummary = taskManager.findSummaryTask(String.valueOf(task.getPersonalTaskId()));
        }
        return new JSP("personal/reply_form_coo.jsp");
    }

    public Resolution view_task() {
        User user = getContext().getCurrentUser();
        id = getContext().getRequest().getParameter("id");
        if (id != null) {
            task = personalTaskDAO.findById(Integer.parseInt(id));
            staff = penggunaDAO.findById(task.getToUser());
            if (task.getDuedate() != null) {
                Date d1 = new Date();
                Date d2 = task.getDuedate();
                LOG.debug("dayLeft1" + d1.getTime());
                LOG.debug("dayLeft" + d2.getTime());
                dayLeft = String.valueOf((d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000));
                LOG.debug(dayLeft);
            }
            listTaskSummary = taskManager.findSummaryTask(String.valueOf(task.getPersonalTaskId()));
        }
        return new JSP("personal/view_task.jsp");
    }

    public Resolution task_close() {
        User user = getContext().getCurrentUser();
        id = getContext().getRequest().getParameter("id");
        if (id != null) {
            task = personalTaskDAO.findById(Integer.parseInt(id));
            staff = penggunaDAO.findById(task.getToUser());
            listTaskSummary = taskManager.findSummaryTask(String.valueOf(task.getPersonalTaskId()));
        }
        return new JSP("personal/task_close.jsp");
    }

    public Resolution autocomplete() {
        StringBuilder results = new StringBuilder();
        userList = userManager.findP();
        if (q != null) {
            for (User user : userList) {
                if (user.getUserId().indexOf(q) != -1) {
                    results.append(user.getUserId()).append("\n");
                }
            }
        }
        return new StreamingResolution("text/plain", results.toString());
    }

    public Resolution savePersonal() {
        User user = getContext().getCurrentUser();
        User u = penggunaDAO.findById(task.getToUser());
        LOG.info(user.getUserId());
        InfoAudit ia = new InfoAudit();
        ia.setCreateDate(new Date());
        ia.setCreateId(user);

        task.setInfoAudit(ia);
        PriorityCode p = priorityCodeDAO.findById(Integer.parseInt(priorityCode));
        if (p != null) {
            task.setPriorityCode(p);
            if (priorityDayCount != null) {
                task.setPriorityDayCount(Integer.parseInt(priorityDayCount));
            } else {
                task.setPriorityDayCount(p.getPriorityDay());
            }
        }
        task.setAssignee(u);
        task.setAcquiredSts("N");
        task.setStatusRemarks("Open");
        task.setFlag(1);
        task.setDeducePoint(0);
        taskManager.saveOrUpdate(task);

        //Task Summary
        taskSummary = new TaskSummary();
        taskSummary.setPersonalTaskId(task.getPersonalTaskId());
        taskSummary.setFromUser(String.valueOf(user.getUserId()));
//        taskSummary.setMessage(StringEscapeUtils.escapeXml(msg));
        taskSummary.setMessage(msg);
        taskSummary.setDateSubmit(new Date());
        taskManager.saveOrUpdateSummary(taskSummary);




        listTaskfile = taskManager.findAllfile(user.getUserId());
        if (listTaskfile != null) {
            Folder folder = taskSummary.getFolder();
            if (folder == null) {
                folder = new Folder();
                taskManager.saveOrUpdateFolder(folder);
                taskSummary.setFolder(folder);
                taskManager.saveOrUpdateSummary(taskSummary);
            }
            for (TaskFile tf : listTaskfile) {
                Document doc = new Document();
                doc.setTitle(tf.getTitle());
                doc.setDocumentPath(tf.getDocumentPath());
                doc.setFolder(folder);
                doc.setContentType(tf.getContentType());
                ia.setCreateDate(new Date());
                ia.setCreateId(getContext().getCurrentUser());
                doc.setInfoAudit(ia);
                taskManager.saveOrUpdateDocument(doc);
                taskManager.deleteFile(tf);
            }
        }
        sendNewPersonalTaskEmail(u.getEmail(), task, taskSummary);
        addSimpleMessage("Success");
        return list_task();
    }

    public Resolution saveReplyForm() {

        String personalTaskId = getContext().getRequest().getParameter("task.personalTaskId");
        User user = getContext().getCurrentUser();
        PersonalTask pt = personalTaskDAO.findById(Integer.parseInt(personalTaskId));
        if (pt != null) {
            InfoAudit ia = pt.getInfoAudit();
            taskSummary = new TaskSummary();
            taskSummary.setMessage(msg);
            taskSummary.setPersonalTaskId(pt.getPersonalTaskId());
            taskSummary.setFromUser(String.valueOf(user.getUserId()));
            taskSummary.setDateSubmit(new Date());
            taskManager.saveOrUpdateSummary(taskSummary);
            User u = penggunaDAO.findById(pt.getInfoAudit().getCreateId().getUserId());

            listTaskfile = taskManager.findAllfile(user.getUserId());
            if (listTaskfile != null) {
                Folder folder = taskSummary.getFolder();
                if (folder == null) {
                    folder = new Folder();
                    taskManager.saveOrUpdateFolder(folder);
                    taskSummary.setFolder(folder);
                    taskManager.saveOrUpdateSummary(taskSummary);
                }
                for (TaskFile tf : listTaskfile) {
                    Document doc = new Document();
                    doc.setTitle(tf.getTitle());
                    doc.setDocumentPath(tf.getDocumentPath());
                    doc.setFolder(folder);
                    doc.setContentType(tf.getContentType());
                    ia.setCreateDate(new Date());
                    ia.setCreateId(getContext().getCurrentUser());
                    doc.setInfoAudit(ia);
                    taskManager.saveOrUpdateDocument(doc);
                    taskManager.deleteFile(tf);
                }
            }

            sendReplyFormTaskEmail(user.getUserId(), u.getEmail(), pt);
            pt.setFlag(0);
            pt.setDuedate(null);
            taskManager.saveOrUpdate(pt);
            addSimpleMessage("Success");
            LOG.debug("finish");


        }
        return new RedirectResolution(PersonalAction.class, "inbox");

    }

    public Resolution saveReplyFormCoo() {
        String personalTaskId = getContext().getRequest().getParameter("task.personalTaskId");
        User user = getContext().getCurrentUser();
        PersonalTask pt = personalTaskDAO.findById(Integer.parseInt(personalTaskId));
        if (pt != null) {
            InfoAudit ia = pt.getInfoAudit();
            ia.setUpdateDate(new Date());
            ia.setUpdateId(user);
            taskSummary = new TaskSummary();
            taskSummary.setMessage(msg);
            taskSummary.setPersonalTaskId(pt.getPersonalTaskId());
            taskSummary.setFromUser(String.valueOf(user.getUserId()));
            taskSummary.setDateSubmit(new Date());
            taskManager.saveOrUpdateSummary(taskSummary);
            listTaskfile = taskManager.findAllfile(user.getUserId());
            if (listTaskfile != null) {
                Folder folder = taskSummary.getFolder();
                if (folder == null) {
                    folder = new Folder();
                    taskManager.saveOrUpdateFolder(folder);
                    taskSummary.setFolder(folder);
                    taskManager.saveOrUpdateSummary(taskSummary);
                }
                for (TaskFile tf : listTaskfile) {
                    Document doc = new Document();
                    doc.setTitle(tf.getTitle());
                    doc.setDocumentPath(tf.getDocumentPath());
                    doc.setFolder(folder);
                    doc.setContentType(tf.getContentType());
                    ia.setCreateDate(new Date());
                    ia.setCreateId(getContext().getCurrentUser());
                    doc.setInfoAudit(ia);
                    taskManager.saveOrUpdateDocument(doc);
                    taskManager.deleteFile(tf);
                }
            }
            User u = penggunaDAO.findById(pt.getToUser());
            sendReplyFormCooTaskEmail(u.getEmail(), pt, taskSummary);
            pt.setFlag(1);
            pt.setInfoAudit(ia);
            if (priorityDayCount != null) {
                pt.setPriorityDayCount(Integer.parseInt(priorityDayCount));
                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                c.add(Calendar.DATE, Integer.parseInt(priorityDayCount));
                pt.setDuedate(c.getTime());
            }
            taskManager.saveOrUpdate(pt);
            calculateDuedateCoo(pt);
        }
        addSimpleMessage("Success");
        LOG.debug("finish");


        return new RedirectResolution(PersonalAction.class, "list_task");
    }

    public Resolution closeTask() {
        String personalTaskId = getContext().getRequest().getParameter("task.personalTaskId");
        User user = getContext().getCurrentUser();
        PersonalTask pt = personalTaskDAO.findById(Integer.parseInt(personalTaskId));
        if (pt != null) {
            InfoAudit ia = pt.getInfoAudit();
            ia.setUpdateDate(new Date());
            ia.setUpdateId(user);
            if (StringUtils.isNotBlank(msg)) {
                taskSummary = new TaskSummary();
                taskSummary.setMessage(msg);
                taskSummary.setPersonalTaskId(pt.getPersonalTaskId());
                taskSummary.setFromUser(String.valueOf(user.getUserId()));
                taskSummary.setDateSubmit(new Date());
                taskManager.saveOrUpdateSummary(taskSummary);
            }
            User u = penggunaDAO.findById(pt.getToUser());
            sendCloseTaskEmail(u.getEmail(), pt, taskSummary);
            pt.setFlag(1);
            pt.setInfoAudit(ia);
            pt.setDuedate(null);
            pt.setStatusRemarks("Close");
            taskManager.saveOrUpdate(pt);
            addSimpleMessage("Success");
            LOG.debug("finish");


        }
        return new RedirectResolution(PersonalAction.class, "list_task");
    }

    public Resolution acquire() {
        User user = getContext().getCurrentUser();
        id = getContext().getRequest().getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            task = personalTaskDAO.findById(Integer.parseInt(id));
            if (task != null) {
                if (task.getAcquiredSts().equals("N")) {
                    InfoAudit ia = task.getInfoAudit();
                    ia.setUpdateDate(new Date());
                    ia.setUpdateId(user);
                    task.setAcquiredSts("Y");
                    Calendar c = Calendar.getInstance();
                    c.setTime(new Date());
                    c.add(Calendar.DATE, task.getPriorityDayCount());
                    task.setDuedate(c.getTime());
                    taskManager.saveOrUpdate(task);


                }
            }
        }
        return new RedirectResolution(PersonalAction.class, "reply_form").addParameter("id", task.getPersonalTaskId());
    }

    public Resolution viewDocuments() {
        User user = getContext().getCurrentUser();
        if (StringUtils.isNotBlank(user.getUserId())) {
            listTaskfile = taskManager.findAllfile(user.getUserId());
        }
        return new JSP("personal/edit_documents.jsp").addParameter("popup", true);
    }

    public Resolution editDocuments() {
        User user = getContext().getCurrentUser();
        if (StringUtils.isNotBlank(user.getUserId())) {
            listTaskfile = taskManager.findAllfile(user.getUserId());
        }
        return new JSP("personal/edit_docsReply.jsp").addParameter("popup", true);
    }

    public Resolution editDocumentsCoo() {
        User user = getContext().getCurrentUser();
        if (StringUtils.isNotBlank(user.getUserId())) {
            listTaskfile = taskManager.findAllfile(user.getUserId());
        }
        return new JSP("personal/edit_docsReplyCoo.jsp").addParameter("popup", true);
    }

    public Resolution saveDocumentsTemp() {
        //TODO : begin.transaction

        String documentPath = conf.getProperty("document.path");
        if (StringUtils.isBlank(documentPath)) {
            return new ErrorResolution(500,
                    "configuration of \"document.path\" not found!");
        }
        InfoAudit ia = new InfoAudit();
        User user = getContext().getCurrentUser();
        User u = penggunaDAO.findById(user.getUserId());
        String path = documentPath + (documentPath.endsWith(File.separator) ? "" : File.separator) + FOLDERNAME + File.separator + u.getUserId() + File.separator;
        String docPath = FOLDERNAME + File.separator + u.getUserId() + File.separator;


        if (document != null) {
            FileUtil fileUtil = new FileUtil(path);
            try {
                fileUtil.saveFile(document.getFileName(), document.getInputStream(), document.getSize());
                TaskFile doc = new TaskFile();
                doc.setTitle(document.getFileName());
                doc.setDocumentPath(docPath + document.getFileName());
                doc.setUserId(u.getUserId());
                doc.setContentType(document.getContentType());
                ia.setCreateDate(new Date());
                ia.setCreateId(getContext().getCurrentUser());
                doc.setInfoAudit(ia);
                taskManager.saveOrUpdateTaskFile(doc);
            } catch (Exception ex) {
                LOG.error("error while saving file {}", ex.getCause().getMessage());


            }
        }


        return new RedirectResolution(PersonalAction.class, "viewDocuments");

    }

    public void calculateDuedateCoo(PersonalTask pt) {

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, pt.getPriorityDayCount());
        pt.setDuedate(c.getTime());
        taskManager.saveOrUpdate(pt);
    }

    public Resolution saveDocumentsReplyTemp() {
        //TODO : begin.transaction

        String documentPath = conf.getProperty("document.path");
        if (StringUtils.isBlank(documentPath)) {
            return new ErrorResolution(500,
                    "configuration of \"document.path\" not found!");
        }
        InfoAudit ia = new InfoAudit();
        User user = getContext().getCurrentUser();
        User u = penggunaDAO.findById(user.getUserId());
        String path = documentPath + (documentPath.endsWith(File.separator) ? "" : File.separator) + FOLDERNAME + File.separator + u.getUserId() + File.separator;
        String docPath = FOLDERNAME + File.separator + u.getUserId() + File.separator;


        if (document != null) {
            FileUtil fileUtil = new FileUtil(path);
            try {
                Folder folder = new Folder();
                taskManager.saveOrUpdateFolder(folder);
                fileUtil.saveFile(document.getFileName(), document.getInputStream(), document.getSize());
                TaskFile doc = new TaskFile();
//                doc.setFolder(folder);
                doc.setTitle(document.getFileName());
                doc.setDocumentPath(docPath + document.getFileName());
                doc.setUserId(u.getUserId());
                doc.setContentType(document.getContentType());
                ia.setCreateDate(new Date());
                ia.setCreateId(getContext().getCurrentUser());
                doc.setInfoAudit(ia);
                taskManager.saveOrUpdateTaskFile(doc);
            } catch (Exception ex) {
                LOG.error("error while saving file {}", ex.getCause().getMessage());


            }
        }


        return new RedirectResolution(PersonalAction.class, "editDocuments");

    }

    public Resolution saveDocumentsReplyCooTemp() {
        //TODO : begin.transaction

        String documentPath = conf.getProperty("document.path");
        if (StringUtils.isBlank(documentPath)) {
            return new ErrorResolution(500,
                    "configuration of \"document.path\" not found!");
        }
        InfoAudit ia = new InfoAudit();
        User user = getContext().getCurrentUser();
        User u = penggunaDAO.findById(user.getUserId());
        String path = documentPath + (documentPath.endsWith(File.separator) ? "" : File.separator) + FOLDERNAME + File.separator + u.getUserId() + File.separator;
        String docPath = FOLDERNAME + File.separator + u.getUserId() + File.separator;


        if (document != null) {
            FileUtil fileUtil = new FileUtil(path);
            try {
                fileUtil.saveFile(document.getFileName(), document.getInputStream(), document.getSize());
                TaskFile doc = new TaskFile();
                doc.setTitle(document.getFileName());
                doc.setDocumentPath(docPath + document.getFileName());
                doc.setUserId(u.getUserId());
                doc.setContentType(document.getContentType());
                ia.setCreateDate(new Date());
                ia.setCreateId(getContext().getCurrentUser());
                doc.setInfoAudit(ia);
                taskManager.saveOrUpdateTaskFile(doc);
            } catch (Exception ex) {
                LOG.error("error while saving file {}", ex.getCause().getMessage());




            }
        }


        return new RedirectResolution(PersonalAction.class, "editDocumentsCoo");

    }

    public Resolution deleteFile() {

        String taskFileId = getContext().getRequest().getParameter("taskFileId");

        if (StringUtils.isNotBlank(taskFileId)) {
            TaskFile tf = taskFileDAO.findById(Integer.parseInt(taskFileId));
            InfoAudit ia = tf.getInfoAudit();
            ia.setUpdateDate(new Date());
            ia.setUpdateId(getContext().getCurrentUser());
            tf.setInfoAudit(ia);
            taskManager.deleteFile(tf);




        }
        return new RedirectResolution(PersonalAction.class, "viewDocuments");

    }

    public Resolution viewDocs() {
        String ids = getContext().getRequest().getParameter("ids");
        if (StringUtils.isNotBlank(ids)) {
            taskSummary = taskSummaryDAO.findById(Integer.parseInt(ids));
        }
        return new JSP("personal/view_documents.jsp").addParameter("popup", true);
    }

    private void sendNewPersonalTaskEmail(String emailAddress, PersonalTask task, TaskSummary taskSummary) {

        if (StringUtils.isNotBlank(emailAddress)) {
            String[] to = {emailAddress};
            String[] ccs = null;
            try {
                freemarker.template.Configuration cfg = new freemarker.template.Configuration();
                FileTemplateLoader templateLoader = new FileTemplateLoader(new File(conf.getProperty("email.template.path")));
                cfg.setTemplateLoader(templateLoader);
                cfg.setObjectWrapper(new DefaultObjectWrapper());
                Template template = cfg.getTemplate("html-new-task.ftl");
                Map<String, Object> rootMap = new HashMap<String, Object>();
                rootMap.put("subject", task.getSubject());
                rootMap.put("message", taskSummary.getMessage());
                rootMap.put("inOffice", conf.getProperty("task.email.inside"));
                rootMap.put("outOffice", conf.getProperty("task.email.outside"));
                Writer out = new StringWriter();
                template.process(rootMap, out);
                emailUtil.send(to, ccs, "ICON OFFSHORE - New Personal Task: " + task.getSubject(), out.toString(), "text/html");
                LOG.debug("finish");
            } catch (Exception ex) {
                LOG.error("error occured {}!! ", ex);
            }
        }
    }

    private void sendReplyFormTaskEmail(String user, String emailAddress, PersonalTask task) {

        if (StringUtils.isNotBlank(emailAddress)) {
            String[] to = {emailAddress};
            String[] ccs = null;
            try {
                freemarker.template.Configuration cfg = new freemarker.template.Configuration();
                FileTemplateLoader templateLoader = new FileTemplateLoader(new File(conf.getProperty("email.template.path")));
                cfg.setTemplateLoader(templateLoader);
                cfg.setObjectWrapper(new DefaultObjectWrapper());
                Template template = cfg.getTemplate("html-replyform-task.ftl");
                Map<String, Object> rootMap = new HashMap<String, Object>();
                rootMap.put("user", user);
                rootMap.put("subject", task.getSubject());
                rootMap.put("inOffice", conf.getProperty("task.email.inside"));
                rootMap.put("outOffice", conf.getProperty("task.email.outside"));
                Writer out = new StringWriter();
                template.process(rootMap, out);
                emailUtil.send(to, ccs, "RE:ICON OFFSHORE - Personal Task: " + task.getSubject(), out.toString(), "text/html");
                LOG.debug("finish");
            } catch (Exception ex) {
                LOG.error("error occured {}!! ", ex);
            }
        }
    }

    private void sendReplyFormCooTaskEmail(String emailAddress, PersonalTask task, TaskSummary taskSummary) {

        if (StringUtils.isNotBlank(emailAddress)) {
            String[] to = {emailAddress};
            String[] ccs = null;
            try {
                freemarker.template.Configuration cfg = new freemarker.template.Configuration();
                FileTemplateLoader templateLoader = new FileTemplateLoader(new File(conf.getProperty("email.template.path")));
                cfg.setTemplateLoader(templateLoader);
                cfg.setObjectWrapper(new DefaultObjectWrapper());
                Template template = cfg.getTemplate("html-replyformCoo-task.ftl");
                Map<String, Object> rootMap = new HashMap<String, Object>();
                rootMap.put("subject", task.getSubject());
                rootMap.put("message", taskSummary.getMessage());
                rootMap.put("inOffice", conf.getProperty("task.email.inside"));
                rootMap.put("outOffice", conf.getProperty("task.email.outside"));
                Writer out = new StringWriter();
                template.process(rootMap, out);
                emailUtil.send(to, ccs, "RE:ICON OFFSHORE - Personal Task: " + task.getSubject(), out.toString(), "text/html");
                LOG.debug("finish");
            } catch (Exception ex) {
                LOG.error("error occured {}!! ", ex);
            }
        }
    }

    private void sendCloseTaskEmail(String emailAddress, PersonalTask task, TaskSummary taskSummary) {

        if (StringUtils.isNotBlank(emailAddress)) {
            String[] to = {emailAddress};
            String[] ccs = null;
            try {
                freemarker.template.Configuration cfg = new freemarker.template.Configuration();
                FileTemplateLoader templateLoader = new FileTemplateLoader(new File(conf.getProperty("email.template.path")));
                cfg.setTemplateLoader(templateLoader);
                cfg.setObjectWrapper(new DefaultObjectWrapper());
                Template template = cfg.getTemplate("html-close-task.ftl");
                Map<String, Object> rootMap = new HashMap<String, Object>();
                rootMap.put("subject", task.getSubject());
                rootMap.put("message", taskSummary.getMessage());
                rootMap.put("inOffice", conf.getProperty("task.email.inside"));
                rootMap.put("outOffice", conf.getProperty("task.email.outside"));
                Writer out = new StringWriter();
                template.process(rootMap, out);
                emailUtil.send(to, ccs, "RE:ICON OFFSHORE - Close Personal Task: " + task.getSubject(), out.toString(), "text/html");
                LOG.debug("finish");
            } catch (Exception ex) {
                LOG.error("error occured {}!! ", ex);
            }
        }
    }

    public Resolution getPriorityDays() {
        String result = "";
        id = getContext().getRequest().getParameter("id");
        result = priorityManager.getPriorityDay(id);
        return new StreamingResolution("text/plan", result);
    }

    public UserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public String getQ() {
        return q;
    }

    public void setQ(String q) {
        this.q = q;
    }

    public PersonalTask getTask() {
        return task;
    }

    public void setTask(PersonalTask task) {
        this.task = task;
    }

    public String getPriorityCode() {
        return priorityCode;
    }

    public void setPriorityCode(String priorityCode) {
        this.priorityCode = priorityCode;
    }

    public PriorityCodeDAO getPriorityCodeDAO() {
        return priorityCodeDAO;
    }

    public void setPriorityCodeDAO(PriorityCodeDAO priorityCodeDAO) {
        this.priorityCodeDAO = priorityCodeDAO;
    }

    public EmailUtil getEmailUtil() {
        return emailUtil;
    }

    public void setEmailUtil(EmailUtil emailUtil) {
        this.emailUtil = emailUtil;
    }

    public PenggunaDAO getPenggunaDAO() {
        return penggunaDAO;
    }

    public void setPenggunaDAO(PenggunaDAO penggunaDAO) {
        this.penggunaDAO = penggunaDAO;
    }

    public TaskManager getTaskManager() {
        return taskManager;
    }

    public void setTaskManager(TaskManager taskManager) {
        this.taskManager = taskManager;
    }

    public List<PersonalTask> getListOfTask() {
        return listOfTask;
    }

    public void setListOfTask(List<PersonalTask> listOfTask) {
        this.listOfTask = listOfTask;
    }

    public List<PersonalTask> getListTask() {
        return listTask;
    }

    public void setListTask(List<PersonalTask> listTask) {
        this.listTask = listTask;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public PersonalTaskDAO getPersonalTaskDAO() {
        return personalTaskDAO;
    }

    public void setPersonalTaskDAO(PersonalTaskDAO personalTaskDAO) {
        this.personalTaskDAO = personalTaskDAO;
    }

    public List<TaskSummary> getListTaskSummary() {
        return listTaskSummary;
    }

    public void setListTaskSummary(List<TaskSummary> listTaskSummary) {
        this.listTaskSummary = listTaskSummary;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public TaskSummary getTaskSummary() {
        return taskSummary;
    }

    public void setTaskSummary(TaskSummary taskSummary) {
        this.taskSummary = taskSummary;
    }

    public TaskSummaryDAO getTaskSummaryDAO() {
        return taskSummaryDAO;
    }

    public void setTaskSummaryDAO(TaskSummaryDAO taskSummaryDAO) {
        this.taskSummaryDAO = taskSummaryDAO;
    }

    public String getPriorityDayCount() {
        return priorityDayCount;
    }

    public void setPriorityDayCount(String priorityDayCount) {
        this.priorityDayCount = priorityDayCount;
    }

    public String getDayLeft() {
        return dayLeft;
    }

    public void setDayLeft(String dayLeft) {
        this.dayLeft = dayLeft;
    }

    public User getStaff() {
        return staff;
    }

    public void setStaff(User staff) {
        this.staff = staff;
    }

    public Configuration getConf() {
        return conf;
    }

    public void setConf(Configuration conf) {
        this.conf = conf;
    }

    public FileBean getDocument() {
        return document;
    }

    public void setDocument(FileBean document) {
        this.document = document;
    }

    public List<PersonalTask> getListTaskHistory() {
        return listTaskHistory;
    }

    public void setListTaskHistory(List<PersonalTask> listTaskHistory) {
        this.listTaskHistory = listTaskHistory;
    }

    public List<PersonalTask> getListTaskHistoryCoo() {
        return listTaskHistoryCoo;
    }

    public void setListTaskHistoryCoo(List<PersonalTask> listTaskHistoryCoo) {
        this.listTaskHistoryCoo = listTaskHistoryCoo;
    }

    public List<TaskFile> getListTaskfile() {
        return listTaskfile;
    }

    public void setListTaskfile(List<TaskFile> listTaskfile) {
        this.listTaskfile = listTaskfile;
    }

    public TaskFileDAO getTaskFileDAO() {
        return taskFileDAO;
    }

    public void setTaskFileDAO(TaskFileDAO taskFileDAO) {
        this.taskFileDAO = taskFileDAO;
    }

    public String getForXlsx() {
        return forXlsx;
    }

    public void setForXlsx(String forXlsx) {
        this.forXlsx = forXlsx;
    }

    public String getForDocx() {
        return forDocx;
    }

    public void setForDocx(String forDocx) {
        this.forDocx = forDocx;
    }

    public String getForPptx() {
        return forPptx;
    }

    public void setForPptx(String forPptx) {
        this.forPptx = forPptx;
    }

    public String getForXls() {
        return forXls;
    }

    public void setForXls(String forXls) {
        this.forXls = forXls;
    }

    public String getForDoc() {
        return forDoc;
    }

    public void setForDoc(String forDoc) {
        this.forDoc = forDoc;
    }

    public String getForPpt() {
        return forPpt;
    }

    public void setForPpt(String forPpt) {
        this.forPpt = forPpt;
    }

    public String getForPdf() {
        return forPdf;
    }

    public void setForPdf(String forPdf) {
        this.forPdf = forPdf;
    }

    public String getForVideoMov() {
        return forVideoMov;
    }

    public void setForVideoMov(String forVideoMov) {
        this.forVideoMov = forVideoMov;
    }

    public String getForVideoMkv() {
        return forVideoMkv;
    }

    public void setForVideoMkv(String forVideoMkv) {
        this.forVideoMkv = forVideoMkv;
    }

    public String getForVideoMp4() {
        return forVideoMp4;
    }

    public void setForVideoMp4(String forVideoMp4) {
        this.forVideoMp4 = forVideoMp4;
    }

    public String getForVideoAvi() {
        return forVideoAvi;
    }

    public void setForVideoAvi(String forVideoAvi) {
        this.forVideoAvi = forVideoAvi;
    }

    public String getForVideoMpeg() {
        return forVideoMpeg;
    }

    public void setForVideoMpeg(String forVideoMpeg) {
        this.forVideoMpeg = forVideoMpeg;
    }

    public String getForVideoWmv() {
        return forVideoWmv;
    }

    public void setForVideoWmv(String forVideoWmv) {
        this.forVideoWmv = forVideoWmv;
    }

    public String getForVideoAsf() {
        return forVideoAsf;
    }

    public void setForVideoAsf(String forVideoAsf) {
        this.forVideoAsf = forVideoAsf;
    }

    public String getForVideoFlv() {
        return forVideoFlv;
    }

    public void setForVideoFlv(String forVideoFlv) {
        this.forVideoFlv = forVideoFlv;
    }

    public String getForVideoRmvb() {
        return forVideoRmvb;
    }

    public void setForVideoRmvb(String forVideoRmvb) {
        this.forVideoRmvb = forVideoRmvb;
    }

    public String getForVideo3gp() {
        return forVideo3gp;
    }

    public void setForVideo3gp(String forVideo3gp) {
        this.forVideo3gp = forVideo3gp;
    }

    public PriorityManager getPriorityManager() {
        return priorityManager;
    }

    public void setPriorityManager(PriorityManager priorityManager) {
        this.priorityManager = priorityManager;
    }

    public List<PersonalTask> getListOpenTask() {
        return listOpenTask;
    }

    public void setListOpenTask(List<PersonalTask> listOpenTask) {
        this.listOpenTask = listOpenTask;
    }

    public List<PersonalTask> getListCloseTask() {
        return listCloseTask;
    }

    public void setListCloseTask(List<PersonalTask> listCloseTask) {
        this.listCloseTask = listCloseTask;
    }
}
